<?php

/**
 * This is the model class for table "yii_comment".
 *
 * The followings are the available columns in table 'yii_comment':
 * @property integer $id
 * @property integer $user_id
 * @property integer $hotel_id
 * @property integer $tour_id
 * @property string $publication_date
 * @property string $header
 * @property string $content
 */
class Comment extends CActiveRecord
{

    private $user;

    public $verification_code;

	/**
	 * Returns the static model of the specified AR class.
	 * @return Comment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'yii_comment';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('content', 'required'),
			array('user_id, hotel_id, tour_id', 'numerical', 'integerOnly'=>true),
			array('publication_date', 'length', 'max'=>10),
			array('header', 'length', 'max'=>100),
            array('verification_code', 'captcha', 'allowEmpty'=>!Yii::app()->user->isGuest),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, hotel_id, tour_id, publication_date, header, content', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'Пользователь',
			'hotel_id' => 'Гостинница',
			'tour_id' => 'Тур',
			'publication_date' => 'Дата публикации',
			'header' => 'Заголовок',
			'content' => 'Содержание',
            'verification_code' => 'Код проверки',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('hotel_id',$this->hotel_id);
		$criteria->compare('tour_id',$this->tour_id);
		$criteria->compare('publication_date',$this->publication_date,true);
		$criteria->compare('header',$this->header,true);
		$criteria->compare('content',$this->content,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}


    public function searchForHotel($hotel_id){
        $criteria=new CDbCriteria;
        
        $criteria->compare('hotel_id',$hotel_id);

        return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
    }

    public function searchForTour($tour_id){
        $criteria=new CDbCriteria;

        $criteria->compare('tour_id',$tour_id);

        return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
    }

    public function getUser(){
        if(!$this->user){
            $user = User::model()->findByPk($this->user_id);
            if(!$user) $user = UserUtil::getAnonymousUser();
            $this->user = $user;
        }

        return $this->user;
    }
}